//
//  ViewController.swift
//  CATransition
//
//  Created by Yinyin Zeng on 24/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let photoImageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: 400, height: 500))
    let tipLabel = UILabel()
    var countImage:Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        photoImageView.center = self.view.center
        photoImageView.contentMode = .scaleAspectFill
        photoImageView.isUserInteractionEnabled = true
        photoImageView.layer.cornerRadius = 20
        photoImageView.layer.masksToBounds = true
        photoImageView.image = UIImage(named:"IMG_00")
        self.view.addSubview(photoImageView)
        
        let swipeLeft = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeAction))
        swipeLeft.direction = .left
        photoImageView.addGestureRecognizer(swipeLeft)
        
        let swipeRight = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeAction))
        swipeRight.direction = .right
        photoImageView.addGestureRecognizer(swipeRight)
        
        let swipetop = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeAction))
        swipetop.direction = .up
        photoImageView.addGestureRecognizer(swipetop)
        
        let swipebottom = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeAction))
        swipebottom.direction = .down
        photoImageView.addGestureRecognizer(swipebottom)
        
    
        let titleLabel = UILabel()
        titleLabel.text = "Popopi"
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: 30)
        titleLabel.frame = CGRect(x: 0, y: 135, width: 100, height: 30)
        titleLabel.center.x = self.view.center.x
        self.view.addSubview(titleLabel)
        
        
        
        tipLabel.text = "傻狗"
        tipLabel.textAlignment = .center
        tipLabel.font = UIFont.systemFont(ofSize: 30)
        tipLabel.isHidden = true
        tipLabel.frame = CGRect(x: 0, y: 725, width: 100, height: 30)
        tipLabel.center.x = self.view.center.x
        self.view.addSubview(tipLabel)
        
        

    }
    
    @objc func swipeAction(sender:UISwipeGestureRecognizer){
        
        
        //创建动画
        let anim = CATransition()
        anim.type = CATransitionType(rawValue: "cube")//pageCurl,pageUnCurl
        
        if sender.direction == .left{//从右往左
            countImage += 1
            anim.subtype = .fromRight
        }else if sender.direction == .right{//从左往右
            countImage -= 1
            anim.subtype = .fromLeft
        }else if sender.direction == .up{
            countImage += 1
            anim.subtype = .fromTop
        }else{
            countImage -= 1
            anim.subtype = .fromBottom
        }
        
        self.photoImageView.layer.add(anim, forKey: "")
        
        if countImage % 8 == 4{
            tipLabel.isHidden = false
        }else{
            tipLabel.isHidden = true
        }
        
        if countImage < 0{
            countImage = 0
        }
        
        
        
        photoImageView.image = UIImage(named: "IMG_0\(countImage%8)")
        
    }


}

